﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL_V1.App_Code.DBBroker;
using EntityObjects;
namespace BAL.Controller
{
    
    public class UpdateStockSupplierControl
    {
        public PurchaseOrderDBBroker pDB;
        public SupplierDBBroker sDB;
        public CatalogueDBBroker cDB;
        public UpdateStockSupplierControl() {
            pDB = PurchaseOrderDBBroker.getPurchaseOrderDBBroker();
            sDB = SupplierDBBroker.getSupplierDBBroker();
            cDB = CatalogueDBBroker.getCatalogueDBBroker();
        }      

        public List<Supplier> getSupplier()
        {
            return sDB.selectAllSupplier();
        }

        public List<PurchaseOrder> getOrder(string t)
        {
            List<PurchaseOrder> lst = new List<PurchaseOrder>();
            List<PurchaseOrder> temp=new List<PurchaseOrder>();
            lst = pDB.selectAllPurchaseOrder();
            foreach (PurchaseOrder po in lst) {
                if (po.SupplierId == t) {
                    temp.Add(po);
                }
            }
            return temp;
        }

        public List<PurchaseOrderDetails> getOrderDetails(string po)
        {
           
           List<PurchaseOrderDetails> list =pDB.selectPODetailsByPOId(po);

           return list;
         
        }

        public bool UpdateData(string data1, int data2,string poNo)
        {
            Catalogue c = cDB.selectCatalogue(data1);
            PurchaseOrder po = pDB.selectPurchaseOrder(poNo);
            PurchaseOrderDetails pod = pDB.selectPurchaseOrderDetails(poNo,data1);
            po.Status = "Delivered";                       
            pod.DeliveredQty = data2;
            c.TotalQty = data2;
            int a1=pDB.updatePurchaseOrder(po, poNo);
            int a2=pDB.updatePurchaseOrderDetails(pod,poNo,data1);
            int a3=cDB.updateCatalogue(c, data1);
            if (a1 == 1 && a2 == 1 && a3 == 1)
            {
                return true;
            }
            else return false;
        }

        public void InsertData()
        {
            Catalogue c = new Catalogue();
            c.ItemId = "T011";
            c.CategoryId = 1;
            c.Description = "Book";
            c.BinId = "20";
            c.ReOrderLevel = 20;
            c.ReOrderQty = 100;
            c.TotalQty = 100;
            c.UOM = "each";

            cDB.addCatalogue(c);
        }
    }
}
